package com.wpf.system.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wpf.system.common.response.ResultUtils;
import com.wpf.system.common.response.ResultVO;
import com.wpf.system.entity.Equipment;
import com.wpf.system.entity.EquipmentBag;
import com.wpf.system.entity.vo.EquipmentBagVO;
import com.wpf.system.service.EquipmentBagService;
import com.wpf.system.service.EquipmentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/equipmentBag")
public class EquipmentBagController {

    @Resource
    private EquipmentBagService equipmentBagService;

    @Resource
    private EquipmentService equipmentService;

    @GetMapping("/{uid}")
    private List<EquipmentBagVO> getEquipmentBagInfo(@PathVariable("uid") Long uid){
        return equipmentBagService.getEquipmentBagInfo(uid);
    }

    @GetMapping("/number/{uid}")
    private int getEquipmentInBagNumber(@PathVariable("uid") Long uid){
        //查询数量之前先清理掉无库存、下架、删除的器材。
        List<Long> ids = new ArrayList<>();
        List<EquipmentBag> equipmentBagList = equipmentBagService.list(new LambdaQueryWrapper<EquipmentBag>().eq(EquipmentBag::getUid,uid));
        if(equipmentBagList.size()>0){
            for(EquipmentBag item : equipmentBagList ){
                Equipment equipment = equipmentService.getById(item.getEid());
                if(ObjectUtil.isNull(equipment)||equipment.getNumber()==0||equipment.getStatus()==0){
                    ids.add(item.getId());
                }
            }
        }
        if(ids.size()>0){
            equipmentBagService.deleteEquipmentBagByIds(ids);
        }
        return equipmentBagList.size()-ids.size();
    }

    @PostMapping
    private ResultVO addEquipmentInBag(@RequestBody EquipmentBag equipmentBag){
        LambdaQueryWrapper<EquipmentBag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EquipmentBag::getEid,equipmentBag.getEid());
        queryWrapper.eq(EquipmentBag::getUid,equipmentBag.getUid());
        Boolean flag = equipmentBagService.getBaseMapper().exists(queryWrapper);
        Boolean result = null;
        if(flag){
            EquipmentBag equipmentBagFromSQL = equipmentBagService.getOne(queryWrapper);
            equipmentBagFromSQL.setNumber(equipmentBagFromSQL.getNumber()+equipmentBag.getNumber());
            result = equipmentBagService.updateById(equipmentBagFromSQL);
        }else{
            //如果用户的器材袋数量大于10则添加失败
            Long num = equipmentBagService.count(new LambdaQueryWrapper<EquipmentBag>().eq(EquipmentBag::getUid,equipmentBag.getUid()));
            if(num<=10){
                result = equipmentBagService.save(equipmentBag);
            }else{
                return ResultUtils.success("器材袋满了！先清理一下器材袋吧！",false);
            }

        }
        if(result){
            return ResultUtils.success("加入器材袋成功！",true);
        }
        return ResultUtils.error("加入器材袋失败！");
    }

    @PutMapping
    private ResultVO saveEquipmentBag(@RequestBody List<EquipmentBag> equipmentBagList){
        if(this.equipmentBagService.updateBatchById(equipmentBagList)){
            return ResultUtils.succcess();
        }else {
            return ResultUtils.error("器材袋信息修改失败！");
        }
    }

    @DeleteMapping("/{ids}")
    private ResultVO deleteEquipmentBagByIds(@PathVariable("ids") String ids){
        List<Long> deleteIds = Convert.toList(Long.class,ids.split(","));
        if(Convert.toBool(equipmentBagService.deleteEquipmentBagByIds(deleteIds))){
            return ResultUtils.success("移出器材袋成功！");
        }else{
            return ResultUtils.error("移出器材袋失败！");
        }
    }
}
